package com.spdzen.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.spdzen.model.Config;
import com.spdzen.service.ConfigService;
import com.spdzen.service.WechatConfigService;
import com.spdzen.support.JsonUtils;
import com.spdzen.support.LogUtils;
import com.spdzen.vo.WechatConfigVO;

/**
 * 通用设置  服务实现类
 */
@Service("wechatConfigService")
public class WechatConfigServiceImpl implements WechatConfigService {
	private static final String CODE_WECHAT_CONFIG = "WECHAT_CONFIG";
	
	@Resource
	private ConfigService configService;
	
	@Override
	public WechatConfigVO selectWechatConfig() {
		Config row = configService.selectByCode(CODE_WECHAT_CONFIG);
		
		WechatConfigVO config = null;
		String content = row.getContent();
		if(StringUtils.isNotBlank(content)){
			try {
				config = JSON.parseObject(content, WechatConfigVO.class);
			} catch (Exception e) {
				LogUtils.logError(e, row);
			}
		}
		
		if(config == null){
			config = new WechatConfigVO();
		}
		
		return config;
	}
	
	@Override
	public int updateWechatConfig(WechatConfigVO vo, Long operateId) {
		Config record = new Config();
		record.setCode(CODE_WECHAT_CONFIG);
		record.setContent(JsonUtils.toJSONString(vo));
		record.setOperateTime(new Date());
		record.setOperateId(operateId);
		
		return configService.updateByCode(record);
	}
}
